package dst1.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityListeners;
import javax.persistence.NamedQuery;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import dst1.listener.ComputerEntityListener;
import dst1.listener.DefaultEntityListener;
import dst1.validator.CPU;

@EntityListeners(ComputerEntityListener.class)

@NamedQuery (
		name = "total_usage_vienna_computer",
		query = "Select c.name, c.location, sum(TIME_TO_SEC(TIMEDIFF( e.end, e.start))) " +
				" from Computer c join c.executions e" +
				" where c.location like 'AUT-VIE%' group by c.name, c.location")
public class Computer implements Serializable{
	
	//Computer has xml mapping (orm.xml)
	
	private Long id;
	
    @Size(min = 5, max = 25, message="Name has to be between 5 and 25 characters long")
	private String name;
	
    @CPU(min = 4, max = 8)
	private Integer cpus;
	
    @Pattern(regexp="[A-Z][A-Z][A-Z]-[A-Z][A-Z][A-Z]@[0-9][0-9][0-9][0-9]+", message="Location has to be in the form: AUT-VIE@1040")
	private String location;
	
	@Past(message="Creation Date has to be in the past")
	private Date creation;
    
	@Past(message="Last Update Date has to be in the past")
	private Date lastUpdate;
	
	private Cluster cluster;
	
	private List<Execution> executions;
	
	public Computer(){
		
	}

	public Computer(String name, Integer cpus, String location,
			Date creation, Date lastUpdate, Cluster cluster) {
		super();
		this.name = name;
		this.cpus = cpus;
		this.location = location;
		this.creation = creation;
		this.lastUpdate = lastUpdate;
		this.cluster = cluster;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getCpus() {
		return cpus;
	}

	public void setCpus(Integer cpus) {
		this.cpus = cpus;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public Date getCreation() {
		return creation;
	}

	public void setCreation(Date creation) {
		this.creation = creation;
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	public Cluster getCluster() {
		return cluster;
	}

	public void setCluster(Cluster cluster) {
		this.cluster = cluster;
	}

	public List<Execution> getExecutions() {
		return executions;
	}

	public void setExecutions(List<Execution> executions) {
		this.executions = executions;
	}
	
}
